package com.demo.springboot底层演示.资源;

import java.io.IOException;
import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.boot.context.config.ConfigFileApplicationListener;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.ResourcePropertySource;
import org.springframework.core.io.support.SpringFactoriesLoader;

public class Test {
	
	public static void main(String[] args) throws Exception {
		SpringApplication app = new SpringApplication(Test.class);
		StandardEnvironment env = new StandardEnvironment();
		env.getPropertySources().addLast(new ResourcePropertySource(new ClassPathResource("settings.properties")));
		for(PropertySource<?> ps : env.getPropertySources()) {
			System.out.println(ps);
		}
		System.out.println(env.getProperty("JAVA_HOME"));
		System.out.println(env.getProperty("basePackage"));
		
		// 加入了application.yml
		ConfigFileApplicationListener postProcssor1 = new ConfigFileApplicationListener();
		postProcssor1.postProcessEnvironment(env, app);
		for(PropertySource<?> ps : env.getPropertySources()) {
			System.out.println(ps);
		}
		System.out.println(env.getProperty("server.port"));
		
		List<String> names = SpringFactoriesLoader.loadFactoryNames(EnvironmentPostProcessor.class, Test.class.getClassLoader());
		for (String name : names) {
			System.out.println(name);
		}
		// 其中ConfigFileApplicationListener就是用来加载application.yml
		names = SpringFactoriesLoader.loadFactoryNames(ApplicationListener.class, Test.class.getClassLoader());
		for (String name : names) {
			System.out.println(name);
		}
	}

}
